;(function ($) {
  'use strict';
  $.dialog = function (option) {
    "use strict";
    var defaults = {
      type: 'panel-default',// panel-primary、panel-success、panel-info、panel-warning、panel-danger
      width: 600,
      title: '',
      content: '',
      btn: [
        {name: '关闭', className: 'btn-default', click:function(){$.closeDialog()}}
      ],
      url: ''
    };
    var params = $.extend(defaults, option);
    // var self = $(this);
    var btnList = params.btn.map(function (item, index) {
      return '<span class="c-dialog-btn btn '+(item.className || "")+'">'+item.name+'</span>';
    }).join("");
    var tpl = '<div class="c-dialog"><div class="c-dialog-box">' +
      '<div class="panel '+params.type+'">' +
      '<div class="panel-heading">'+(params.title?params.title:'')+'<span class="glyphicon glyphicon-remove"></span></div>' +
      (params.content?'<div class="panel-body">'+params.content+'</div>':'') +
      (params.url?'<div class="panel-body"><iframe src="'+params.url+'" frameborder="0" width="100%" height="'+($(window).height()*0.6)+'"></iframe></div>':'') +
      '<div class="panel-footer">'+(params.btn.length>0?btnList:'')+'</div>' +
      '</div>' +
      '</div></div>';
    $("body").append(tpl);
    var dialog = $(".c-dialog");
    dialog.find(".c-dialog-box").css("width", params.width);
    // 按钮事件
    $(".c-dialog-btn",dialog).each(function(i,e){
      $(e).on("click", function(){
        if(params.btn[i].click){
          params.btn[i].click($(e));
        }
        
      })
    });
    // 关闭按钮
    $(".glyphicon-remove", dialog).on("click", function () {
      $.closeDialog();
    });
    $.closeDialog = function(){
      dialog.remove();
    };
  }
})(jQuery);

// toast
;(function($) {
  "use strict";
  $.toast = function(text,callback) {
    if($(".e-toast").length == 0){
      var tpl = '<div class="e-toast">' +
        '<p class="toast-text">'+text+'</p>' +
        '</div>';
      var dialog = $(tpl).appendTo(document.body);
      dialog.fadeIn().delay(1500).fadeOut();
      setTimeout(function() {
        if(callback){
          callback($(this));
        }
        $(".e-toast").remove();
      }, 2500);
    }
  };
})(jQuery);

// showImg
;(function($) {
  "use strict";
  $.showImg = function(url) {
    var tpl = '<div class="show-img-popup"><div class="img-box">'+
    '<i class="glyphicon glyphicon-remove"></i><img src="'+url+'">'+
    '</div></div>';
    var popup = $(tpl).appendTo(document.body);
    $(".glyphicon-remove", popup).on("click", function () {
      popup.remove();
    });
  };
})(jQuery);

// copyFirst
;(function ($) {
  "use strict";
  $.coypFirst = function (option) {
    var defaults = {
      ele: '.js-copy-first',
      item: '.copy-item',
      add: '.add-btn',
      delete: '.delete-btn',
      addCallBack:function(){},
      deleteCallBack:function(){}
    };
    var params = $.extend(defaults, option);
    var self = $(params.ele);
    $(params.add, self).on("click", function () {
      var list = $(params.item, self);
      list.parent().append(list.eq(0).prop("outerHTML"));
      params.addCallBack();
    })
    $(params.delete, self).on("click", function () {
      var list = $(params.item, self);
      if(list.length>1){
        list.last().remove();
      }
      params.deleteCallBaclBack();
    })
  }
})(jQuery)

// loading
;(function () {
  $.loading = function () {
    var tpl = '<div class="loading-box"><span class="loading-gif"></span></div>';
    if ($(".loading-gif").length === 0){
      var load = $(tpl).appendTo(document.body);
    }
    $.loadingHide = function () {
      load.remove();
    }
  }
})(jQuery)

// 图片上传
;(function ($) {
  "use strict";
  $.fn.uploadImg = function (option) {
    var defaults = {
      max: null,
      curList: [],
      callback: function (file) {
        console.log(file)
      }
    };
    var params = $.extend(defaults, option);
    var self = $(this);
    var tpl = '<ul class="upload-img">'+
    '<li class="upload-btn">'+
    '<i class="glyphicon glyphicon-upload"></i>'+
    '<input type="file" multiple="multiple"></li></ul>';
    self.append(tpl);
    var fileList = null;
    $(".upload-btn input", self).on("click", function () {
      $(this).val('')
    })
    $(".upload-btn input", self).on("change", function () {
      var that = this;
      var fileNum = that.files.length;
      var curLen = $("li", self).length - 1;
      if (params.max) {
        if (fileNum + curLen <= params.max) {
          upload();
        }else{
          $.toast('最多上传'+params.max+'张');
        }
      } else {
        upload();
      }
      function upload(){
        fileList = [];
        for (var i=0;i<fileNum;i++) {
          fileList.push(that.files[i])
          var url = getObjectURL(that.files[i]);
          $(that).parent().before('<li><div class="img-item"><p class="show-img">查看大图</p><span class="delete-img"><i class="glyphicon glyphicon-remove"></i></span><img src="'+url+'"></li></div>');
        }
        params.curList = params.curList.concat(fileList);
        params.callback(params.curList)
      }
    })
    self.on("click", ".show-img", function () {
      var url = $(this).parent().find("img").attr("src");
      $.showImg(url);
    })
    self.on("click", ".delete-img", function () {
      var li = $(this).parent().parent();
      params.curList.splice(li.index(), 1)
      li.remove();
      params.callback(params.curList)
    })
    function getObjectURL(file) {
      var url = null;
      if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
      } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
      } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
      }
      return url;
    }
  }
})(jQuery)